import { _decorator, Animation } from 'cc';
import { ENTITY_TYPE_ENUM, PARAMS_NAME_ENUM, SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM } from '../../Enums';
import { getInitParamsNumber, StateMachine } from '../../Base/StateMachine';
import SpikeOneSubManchine from './SpikeOneSubManchine';
import SpikeTwoSubManchine from './SpikeTwoSubManchine';
import SpikeThreeSubManchine from './SpikeThreeSubManchine';
import SpikeFourSubManchine from './SpikeFourSubManchine';
import { SpikesManager } from './SpikesManager';
const { ccclass, property } = _decorator;


@ccclass('SpikesStateMachine')
export class SpikesStateMachine extends StateMachine {


    async init() {
        this.animationComponent = this.node.addComponent(Animation);
        this.initParams();
        this.initStateMachines();
        this.initAnimationEvent();

        await Promise.all(this.waitingList);
    }

    initParams() {
        this.params.set(PARAMS_NAME_ENUM.SPIKES_CUR_COUNT, getInitParamsNumber());
        this.params.set(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT, getInitParamsNumber());
    }

    initStateMachines() {
        this.statMachines.set(ENTITY_TYPE_ENUM.SPIKES_ONE, new SpikeOneSubManchine(this));
        this.statMachines.set(ENTITY_TYPE_ENUM.SPIKES_TWO, new SpikeTwoSubManchine(this));
        this.statMachines.set(ENTITY_TYPE_ENUM.SPIKES_THREE, new SpikeThreeSubManchine(this));
        this.statMachines.set(ENTITY_TYPE_ENUM.SPIKES_FOUR, new SpikeFourSubManchine(this));

    }

    initAnimationEvent() {
        this.animationComponent.on(Animation.EventType.FINISHED, () => {
            const name = this.animationComponent.defaultClip.name;

            const value = this.getParams(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT);
            if ((value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_ONE && name.includes('spikesone/two')) ||
                (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_TWO && name.includes('spikestwo/three')) ||
                (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_THREE && name.includes('spikesthree/four')) ||
                (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_FOUR && name.includes('spikesfour/five'))

            ) {
                this.node.getComponent(SpikesManager).backZero();
            }
            // const whiteList = ['attack'];
            // if (whiteList.some(v => name.includes(v))) {
            //     this.node.getComponent(EntityManager).state = ENTITY_STATE_ENUM.IDLE;
            // }
        })
    }

    run() {
        const value = this.getParams(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT);

        switch (this.currentState) {
            case this.statMachines.get(ENTITY_TYPE_ENUM.SPIKES_ONE):
            case this.statMachines.get(ENTITY_TYPE_ENUM.SPIKES_TWO):
            case this.statMachines.get(ENTITY_TYPE_ENUM.SPIKES_THREE):
            case this.statMachines.get(ENTITY_TYPE_ENUM.SPIKES_FOUR):
                if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_ONE) {
                    this.currentState = this.statMachines.get(ENTITY_TYPE_ENUM.SPIKES_ONE);
                } else if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_TWO) {
                    this.currentState = this.statMachines.get(ENTITY_TYPE_ENUM.SPIKES_TWO);
                } else if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_THREE) {
                    this.currentState = this.statMachines.get(ENTITY_TYPE_ENUM.SPIKES_THREE);
                } else if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_FOUR) {
                    this.currentState = this.statMachines.get(ENTITY_TYPE_ENUM.SPIKES_FOUR);
                } else {
                    this.currentState = this.currentState;
                }
                break;
            default:
                this.currentState = this.statMachines.get(ENTITY_TYPE_ENUM.SPIKES_ONE);
        }
    }
}


